class Cartasur::Visited < Cartasur::CartasurModel

  has_many :visiteds_images, :class_name => 'Cartasur::VisitedsImage'
  belongs_to :protocol, :class_name => 'Cartasur::Protocol'
  belongs_to :track, :class_name => 'Cartasur::Track'

  attr_accessible :objective_id, :user_id, :track_id, :protocol_id, :visiteds_images_attributes, :protocol_attributes
  accepts_nested_attributes_for :visiteds_images, :reject_if => :all_blank
  accepts_nested_attributes_for :protocol, :reject_if => :all_blank


  def user_login
    user = Cartasur::User.find(self.user_id)
    user.login unless user.nil?
  end
  def objective_details
    objective = Cartasur::Objective.find(self.objective_id)
    objective.time_range #TODO: return something readable
  end
  def objective_place
    objective = Cartasur::Objective.find(self.objective_id)
    objective.try(:location_name)
  end
  def track_name
    track = Cartasur::Track.find(self.track_id)
    track.name
  end

  #class methods
  def self.get_by_user_and_objective(user_id,objective_id)
    where("user_id = #{user_id} and objective_id = #{objective_id}").all
  end
end